package com.dao;

import com.entity.book_examine;
import com.entity.search;
import com.utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class book_examineDao {
    public void add(book_examine newbook) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into book_examine(bookname,author_name,types,brief_introduction) values(?,?,?,?)";
        queryRunner.update(sql,newbook.getBookname(),newbook.getAuthor_name(),newbook.getTypes(),newbook.getBrief_introduction());
    }
    public List<book_examine> findall() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from book_examine ";
        List<book_examine> list = queryRunner.query(sql, new BeanListHandler<>(book_examine.class));
        return list;
    }

    public List<book_examine> findbyauthor(String author) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from book_examine where author_name = ?";
        List<book_examine> list = queryRunner.query(sql, new BeanListHandler<>(book_examine.class),author);
        return list;
    }

    public book_examine findbybookname(String bookname) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from book_examine where bookname = ?";
        book_examine book = queryRunner.query(sql, new BeanHandler<>(book_examine.class),bookname);
        return book;
    }
    public void delBybookname(String bookname) throws SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "delete from book_examine where bookname = ?";
        queryRunner.update(sql,bookname);
    }


}
